//
// DO NOT MODIFY THIS FILE - it is generated using templatec on Generator.t
//
package lpg.tool.templatec;

import java.io.PrintWriter;
import lpg.util.Utility; /* Generator.t:1 */

class Generator
{
    private PrintWriter out = null;

    public void setOut(PrintWriter pw)
    {
        out = pw;
    }

    public void run()
    {
        out.write("\n//--"); /* Generator.t:1~2 */
        out.print(control.classFile.getAbsolutePath()); /* Generator.t:2 */
        out.write("\n//\n// DO NOT MODIFY THIS FILE - it is generated using templatec on "); /* Generator.t:2~4 */
        out.print(control.templateFile.getName()); /* Generator.t:4 */
        out.write("\n//\n"); /* Generator.t:4~6 */

        if (control.packageOpt != null) /* Generator.t:8 */
        { /* Generator.t:9 */
            out.write("package "); /* Generator.t:10 */
            out.print(control.packageOpt); /* Generator.t:10 */
            out.write(";\n\n"); /* Generator.t:10~12 */
        } /* Generator.t:13 */

        out.write("import java.io.PrintWriter;"); /* Generator.t:15 */
        if (!control.importList.isEmpty()) /* Generator.t:16 */
        { /* Generator.t:17 */
            for (Control.Element importName : control.importList) /* Generator.t:18 */
            { /* Generator.t:19 */
                out.write("\nimport "); /* Generator.t:20~21 */
                out.print(importName.content); /* Generator.t:21 */
                out.write("; /* "); /* Generator.t:21 */
                out.print(importName.fileName); /* Generator.t:21 */
                out.write(":"); /* Generator.t:21 */
                out.print(importName.endLineNo); /* Generator.t:21 */
                out.write(" */"); /* Generator.t:21 */
            } /* Generator.t:22 */
        } /* Generator.t:23 */
        out.write("\n\n"); /* Generator.t:24~26 */

        if (control.modifierOpt != null) /* Generator.t:28 */
        { /* Generator.t:29 */
            out.print(control.modifierOpt.content); /* Generator.t:30 */
            out.write(" /* "); /* Generator.t:30 */
            out.print(control.modifierOpt.fileName); /* Generator.t:30 */
            out.write(":"); /* Generator.t:30 */
            out.print(control.modifierOpt.endLineNo); /* Generator.t:30 */
            out.write(" */ "); /* Generator.t:30 */
        } /* Generator.t:31 */

        out.write("class "); /* Generator.t:33 */
        out.print(control.className); /* Generator.t:33 */

        if (control.extendsOpt != null) /* Generator.t:35 */
        { /* Generator.t:36 */
            out.write(" extends "); /* Generator.t:37 */
            out.print(control.extendsOpt.content); /* Generator.t:37 */
            out.write(" /* "); /* Generator.t:37 */
            out.print(control.extendsOpt.fileName); /* Generator.t:37 */
            out.write(":"); /* Generator.t:37 */
            out.print(control.extendsOpt.endLineNo); /* Generator.t:37 */
            out.write(" */"); /* Generator.t:37 */
        } /* Generator.t:38 */

        if (!control.implementsList.isEmpty()) /* Generator.t:40 */
        { /* Generator.t:41 */
            out.write(" implements "); /* Generator.t:42 */
            for (Control.Element implementsName : control.implementsList) /* Generator.t:43 */
            { /* Generator.t:44 */
                if (first) /* Generator.t:45 */
                { /* Generator.t:46 */
                    first = false; /* Generator.t:47 */
                } /* Generator.t:48 */
                else /* Generator.t:49 */
                { /* Generator.t:50 */
                    out.write(", "); /* Generator.t:51 */
                } /* Generator.t:52 */
                out.print(implementsName.content); /* Generator.t:53 */
                out.write(" /* "); /* Generator.t:53 */
                out.print(implementsName.fileName); /* Generator.t:53 */
                out.write(":"); /* Generator.t:53 */
                out.print(implementsName.endLineNo); /* Generator.t:53 */
                out.write(" */"); /* Generator.t:53 */
            } /* Generator.t:54 */
        } /* Generator.t:55 */

        out.write("\n{\n    private PrintWriter out = null;\n\n    public void setOut(PrintWriter pw)\n    {\n        out = pw;\n    }\n"); /* Generator.t:57~65 */
        if (control.mainOpt == null || control.mainOpt.content.length() > 0) /* Generator.t:66 */
        { /* Generator.t:67 */
            out.write("\n    "); /* Generator.t:68~69 */
            if (control.mainOpt == null) /* Generator.t:70 */
            { /* Generator.t:71 */
                out.write("public void run()"); /* Generator.t:72 */
            } /* Generator.t:73 */
            else /* Generator.t:74 */
            { /* Generator.t:75 */
                out.print(control.mainOpt.content); /* Generator.t:76 */
                out.write(" /* "); /* Generator.t:76 */
                out.print(control.mainOpt.fileName); /* Generator.t:76 */
                out.write(":"); /* Generator.t:76 */
                out.print(control.mainOpt.endLineNo); /* Generator.t:76 */
                out.write(" */"); /* Generator.t:76 */
            } /* Generator.t:77 */
            out.write("\n    {\n        "); /* Generator.t:78~80 */
            first = true; /* Generator.t:81 */
            for (Control.Element element : control.mainList) /* Generator.t:82 */
            { /* Generator.t:83 */
                generateElement(element); /* Generator.t:84 */
            } /* Generator.t:85 */
        out.write("\n    }\n"); /* Generator.t:86~88 */
        } /* Generator.t:89 */

        for (Control.Element element : control.memberList) /* Generator.t:91 */
        { /* Generator.t:92 */
            generateElement(element); /* Generator.t:93 */
        } /* Generator.t:94 */
        out.write("\n}\n"); /* Generator.t:95~97 */
    }

    private boolean first = true; /* Generator.t:98 */
    private int indent = 8; // Default indent in run() /* Generator.t:99 */

    private final Control control; /* Generator.t:101 */

    Generator(Control ctrl, PrintWriter writer) /* Generator.t:103 */
    { /* Generator.t:104 */
        control = ctrl; /* Generator.t:105 */
        setOut(writer); /* Generator.t:106 */
    } /* Generator.t:107 */

    private void generateElement(Control.Element element) /* Generator.t:109 */
    { /* Generator.t:110 */
        switch (element.state) /* Generator.t:111 */
        { /* Generator.t:112 */
            case ACTION: /* Generator.t:113 */
                indent(); /* Generator.t:114 */
                first = false; /* Generator.t:115 */
                out.write("out.write("); /* Generator.t:116 */
                out.print(Utility.escape(element.content, '"')); /* Generator.t:116 */
                out.write("); /* "); /* Generator.t:116 */
                out.print(element.fileName); /* Generator.t:116 */
                out.write(":"); /* Generator.t:116 */
                out.print(element.startLineNo); /* Generator.t:116 */
                if (element.startLineNo != element.endLineNo) /* Generator.t:117 */
                { /* Generator.t:118 */
                    out.write("~"); /* Generator.t:119 */
                    out.print(element.endLineNo); /* Generator.t:119 */
                } /* Generator.t:120 */
                out.write(" */"); /* Generator.t:121 */
                break; /* Generator.t:122 */
            case EXPRESSION: /* Generator.t:123 */
                indent(); /* Generator.t:124 */
                first = false; /* Generator.t:125 */
                out.write("out.print("); /* Generator.t:126 */
                out.print(element.content); /* Generator.t:126 */
                out.write("); /* "); /* Generator.t:126 */
                out.print(element.fileName); /* Generator.t:126 */
                out.write(":"); /* Generator.t:126 */
                out.print(element.endLineNo); /* Generator.t:126 */
                out.write(" */"); /* Generator.t:126 */
                break; /* Generator.t:127 */
            case DECLARATION: /* Generator.t:128 */
            case SCRIPTLET: /* Generator.t:129 */
                indent = element.endColumnNo; /* Generator.t:130 */
                first = true; /* Generator.t:131 */
                out.print(element.content); /* Generator.t:132 */
                break; /* Generator.t:133 */
            default: /* Generator.t:134 */
                assert false; /* Generator.t:135 */
        } /* Generator.t:136 */
    } /* Generator.t:137 */

    // There is no way to format perfectly, just try our best /* Generator.t:139 */
    private void indent() /* Generator.t:140 */
    { /* Generator.t:141 */
        if (!first) /* Generator.t:142 */
        { /* Generator.t:143 */
            out.write("\n"); /* Generator.t:144~145 */
            for (int i = 0; i < indent; i++) /* Generator.t:146 */
            { /* Generator.t:147 */
                out.write(" "); /* Generator.t:148 */
            } /* Generator.t:149 */
        } /* Generator.t:150 */
    } /* Generator.t:151 */

}
